import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tiktokadult/hj_page/mine_business/ai/mode/ai_record_entity.dart';
import 'package:tiktokadult/hj_page/mine_business/ai/widget/ai_record_widget.dart';
import 'package:tiktokadult/hj_page/mine_business/widgets/common_alert.dart';
import 'package:tiktokadult/hj_utils/api_service/ai_service.dart';
import 'package:tiktokadult/hj_utils/global.dart';

//ai生成记录base类
abstract class AIRecordBaseLogic extends GetxController with GetTickerProviderStateMixin {
  AIRecordBaseLogic(this.aiType);
  final dataSource = [];
  int page = 1;
  int sort = 1;
  final int aiType;
  RefreshController? refreshController;
  bool isLoading = true;
  final subTypes = ['排队', '成功', '失败'];
  late final TabController tabController = TabController(length: subTypes.length, vsync: this);

  @override
  void onReady() {
    super.onReady();
    fetchPageData();
  }

  @mustCallSuper
  fetchPageData({bool isRefresh = true, bool needLoading = false}) {
    if (isRefresh) {
      page = 1;
    }
    if (needLoading) {
      isLoading = true;
      update();
    }
  }

  Widget instanceChild(int index) => AIRecordWidget(
        dataSource[index],
        aitype: aiType,
        onDeleteCallback: () => deleteRecord(index),
      );

  @mustCallSuper
  Future<bool> deleteRecord(int index) async {
    final value = await Get.dialog(CommonAlert(
      desc: '是否删除该生成记录?',
      leftBtn: '取消',
      rightBtn: '确定',
    ));
    return value;
  }
}

/// 脱衣历史记录
class AIStripRecordLogic extends AIRecordBaseLogic {
  AIStripRecordLogic(super.aiType);

  @override
  fetchPageData({bool isRefresh = true, bool needLoading = false}) async {
    super.fetchPageData(isRefresh: isRefresh, needLoading: needLoading);
    final res = await AIService.getAiRecordList(page, 10, sort);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    dataSource.addAll(res?.list ?? []);
    res?.hasNext == true ? refreshController?.loadComplete() : refreshController?.loadNoData();
    page += 1;
    update();
  }

  @override
  Future<bool> deleteRecord(int index) async {
    final res = await super.deleteRecord(index);
    final model = dataSource[index] as AiRecordModel;
    if (res == true) {
      final result = await AIService.deleteAiBill(model.id);
      if (result) {
        dataSource.removeAt(index);
        showToast('删除成功');
        update();
        return result;
      }
    }
    return false;
  }
}

/// 视频换脸
class AIVCFRecordLogic extends AIRecordBaseLogic {
  AIVCFRecordLogic(super.aiType);

  @override
  fetchPageData({bool isRefresh = true, bool needLoading = false}) async {
    super.fetchPageData(isRefresh: isRefresh, needLoading: needLoading);

    final status = [0, 1, -1];
    final res = await AIService.getAiFaceRecordList(page, 10, status[sort - 1]);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }

    dataSource.addAll(res?.list ?? []);
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    page += 1;
    page += 1;
    update();
  }

  @override
  Future<bool> deleteRecord(int index) async {
    final res = await super.deleteRecord(index);
    final model = dataSource[index] as AiRecordModel;
    if (res == true) {
      final result = await AIService.deleteAiFaceBill(model.id);
      if (result) {
        dataSource.removeAt(index);
        showToast('删除成功');
        update();
        return result;
      }
    }
    return false;
  }
}

/// 图片换脸
class AIICFRecordLogic extends AIRecordBaseLogic {
  AIICFRecordLogic(super.aiType);

  @override
  fetchPageData({bool isRefresh = true, bool needLoading = false}) async {
    super.fetchPageData(isRefresh: isRefresh, needLoading: needLoading);
    final res = await AIService.getAiImgFaceRecordList(page, 10, sort);
    isLoading = false;
    if (isRefresh) {
      dataSource.clear();
      refreshController?.refreshCompleted();
    }
    dataSource.addAll(res?.list ?? []);

    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    page += 1;
    update();
  }

  @override
  Future<bool> deleteRecord(int index) async {
    final res = await super.deleteRecord(index);
    final model = dataSource[index] as AiRecordModel;
    if (res == true) {
      final result = await AIService.deleteAiFaceImgBill(model.id);
      if (result) {
        dataSource.removeAt(index);
        showToast('删除成功');
        update();
        return result;
      }
    }
    return false;
  }
}

AIRecordBaseLogic instanceController(int type) {
  if (type == 0) return AIStripRecordLogic(type);
  if (type == 1) return AIVCFRecordLogic(type);
  return AIICFRecordLogic(type);
}
